#include <vector>
#include <string>
#include <iostream>

#include "function.h"
#include "util.h"


using namespace std;

Function::Function(string Name,int Position)
{
///Setup the data for initialization
PositionInFile=Position;///line number in the file at which the function begins
NameParametrized=Name;///name with parameters of the function
ScopePointer=0;

///temp variables to help break up the function
unsigned int temp=0;
int SpaceCount=0;
while(Name[temp]==' ')///making sure that the function doesn't start with white spaces
	temp++;

///Cleaning the recieved line which is the beginning of the function with parameters and here we try to get it without parameters
while(temp<Name.length())
	{
	if(Name[temp]==' ')///couting the spaces since the first space we encounter now mean the function name already ended
		SpaceCount++;
	if(Name[temp]=='(')///function name ended
		break;
	if((Name[temp]!=' ')&&(SpaceCount==1))
		NameOnly+=Name[temp];

	temp++;
	}
///For future use if we want to differentiate between scope of a higher level
Scopes.push_back(Name);
///Push first line in the function
Lines.push_back(NameParametrized);
///Push scope of first line in the function
LineScopes.push_back(ScopePointer);
}

Function::~Function()
{
}

bool Function::AddLine(string Line)
{
//break at ( or ) or { or }
// Tried to clean the code myself, but then thought it would change the line reference so decided to assume that the programmer will have a good coding style
unsigned int temp=0;
//string TempLine="";
bool InFunction=true;//,PushedLine=false;

///Will simply make sure we are still in the function and increments/decrements the scope
while(temp<Line.length())
	{
//	TempLine+=Line[temp];
//	PushedLine=false;
	if((Line[temp]=='{')||(Line[temp]=='}'))//increment/decrement scope
		{
		if((Line[temp]=='{'))
			{
			ScopePointer++;
			}

		if((Line[temp]=='}'))
			{
			ScopePointer--;
			if(ScopePointer==-1)//Function Ended
				InFunction=false;
			}

		}
	temp++;
	}

///Push back current line and its scope
Lines.push_back(Line);
LineScopes.push_back(ScopePointer);

return InFunction;
}

string Function::GetLines(bool Numbered)
{
string LinesString="";

///Return the lines of the function numbered
if(Numbered==true)
	{
	unsigned int temp=0;
	while(temp<Lines.size())
		{
		LinesString+=ConvertIntToString(temp);
		LinesString+=") ";
		LinesString+=Lines.at(temp);
		LinesString+="\n";
		temp++;
		}	
	}
else if(Numbered==false)///Return the lines of the function unnumbered
	{
	unsigned int temp=0;
        while(temp<Lines.size())
                {
                LinesString+=Lines.at(temp);
                LinesString+="\n";
		temp++;
                }    
	}

return LinesString;
}

string Function::GetNameParametrized()
{
return NameParametrized; 
}

string Function::GetLineAtPosition(int LinePosition)
{
return Lines.at(LinePosition);
}

int Function::GetScopeOfLineAtPosition(int LinePosition)
{
return LineScopes.at(LinePosition);
}

string Function::GetNameOnly()
{
return NameOnly;
}

int Function::GetPositionInFile()
{
return PositionInFile;
}

int Function::GetLineCount()
{
return Lines.size();
}
